package com.loiane.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.loiane.dataaccess.CheckURLDatabaseAccessor;
import com.loiane.exception.CheckURLException;
import com.loiane.model.TransformerItem;
import com.loiane.model.URLCheckingResult;
import com.loiane.service.CheckURLService;

@Controller
public class CheckURLsConfigController {
	
	@RequestMapping("/applycfg.do")
	public void applyCheckURLsConfigration(){
		System.err.println("invoke the apply button..");
		
	}
	
	@RequestMapping("/urlcreate.do")
	public @ResponseBody String urlCreate(@RequestParam(value="isavailable") int isavailable){
		System.err.println("invoke url create......");
		Map<String,Object> map = new HashMap<String,Object>();
		
		map.put("success", true);
		JSONObject jso = JSONObject.fromObject(map);
		return jso.toString();		
	}
	
	@RequestMapping("/urlupdate.do")	
	public @ResponseBody String urlUpdate(@RequestBody String names){
		System.err.println("invoke url update......");
		Object obj = JSONObject.fromObject(names).get("names");
		TransformerItem ti = null;
		
		if(obj instanceof JSONArray){
			@SuppressWarnings("unchecked")
			Iterator<JSONObject> it = ((JSONArray) obj).iterator();
			while(it.hasNext()){
				JSONObject js = it.next();
				ti = new TransformerItem();
				ti.setId(Integer.valueOf(js.get("id").toString()));
				ti.setName(js.get("name").toString());
			}
		}else if(obj instanceof JSONObject){
			
		}
		return names;
	}
	
	@RequestMapping("/urldestroy.do")
	public @ResponseBody String urlDestory(@RequestParam(value="isavailable") int isavailable){
		System.err.println("invoke url destroy......");
		Map<String,Object> map = new HashMap<String,Object>();
		
		map.put("success", true);
		JSONObject jso = JSONObject.fromObject(map);
		return jso.toString();
		
	}
	
	@RequestMapping("/urlread.do")
	public @ResponseBody String urlRead(@RequestParam(value="isavailable") int isavailable){
		System.err.println("invoke url read......");
		Map<String,Object> map = new HashMap<String,Object>();
		try{
			CheckURLService cs = new CheckURLDatabaseAccessor();
			List<TransformerItem> urls = cs.getURLs(isavailable);
			map.put("success", true);
			map.put("names", urls);
		}catch(CheckURLException e){
			map.put("success", false);
		}

		JSONObject jso = JSONObject.fromObject(map);
		
		return jso.toString();
	}
	
	@RequestMapping("/checkURL.do")
	public @ResponseBody String checkURLs(){
		System.err.println("invoke url checking ......");
		Map<String,Object> map = new HashMap<String,Object>();	
		List<URLCheckingResult> result = new ArrayList<URLCheckingResult>();
		CheckURLService cs = new CheckURLDatabaseAccessor();
//		try {
//			result = cs.checkURLs();
			URLCheckingResult ur1 = new URLCheckingResult();
			ur1.setAvailable(false);
			ur1.setUrl("http://163.com");
			result.add(ur1);
			
			URLCheckingResult ur2 = new URLCheckingResult();
			ur2.setAvailable(true);
			ur2.setUrl("http://localhost:8080/producthealthtool");
			result.add(ur2);
			
			map.put("success", true);			
			map.put("checking", result);
//		} catch (CheckURLException e) {
//			e.printStackTrace();
//			map.put("success", false)	;	
//		}
		JSONObject jso = JSONObject.fromObject(map);
		return jso.toString();
	}
}
